//
//  oldRegiterViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/10.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldRegiterViewController.h"
#import "oldRegex.h"
#import "oldBaseViewController.h"
#import "oldKeyChain.h"
#import "AppDelegate.h"
#import "QNuploader.h"

@interface oldRegiterViewController ()
{
    NSUInteger next_time;
}

#define MALE @"1"
#define FEMALE @"2"

@property (weak, nonatomic) IBOutlet UITextField *text_phoneNum;
@property (weak, nonatomic) IBOutlet UIButton *btn_validcode;
@property (weak, nonatomic) IBOutlet UITextField *text_validcode;
@property (weak, nonatomic) IBOutlet UITextField *text_password;
@property (weak, nonatomic) IBOutlet UITextField *text_confirm;
@property (weak, nonatomic) IBOutlet UITextField *text_name;
@property (weak, nonatomic) IBOutlet UITextField *text_certification;
@property (weak, nonatomic) IBOutlet UIButton *btn_next;

@property (nonatomic, strong)NSTimer *timer;

@end

@implementation oldRegiterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    _btn_validcode.layer.cornerRadius = 6.f;
    _btn_next.layer.cornerRadius = 6.f;
    self.navigationItem.title = @"注册";
    [_btn_next addTarget:self action:@selector(gotoNext:) forControlEvents:UIControlEventTouchDown];
    [_btn_validcode addTarget:self action:@selector(sendSMS:) forControlEvents:UIControlEventTouchDown];
}

#pragma mark - func

//- (void)btn_male_tapped:(id)sender
//{
//    [self sexChoose:YES];
//}
//
//- (void)btn_female_tapped:(id)sender
//{
//    [self sexChoose:NO];
//}

//- (void)sexChoose:(BOOL)male
//{
//    if (male) {
//        [_btn_male setImage:[UIImage imageNamed:@"性别选中"] forState:UIControlStateNormal];
//        [_btn_female setImage:[UIImage imageNamed:@"性别未选中"] forState:UIControlStateNormal];
//        _label_male.textColor = OLD_COLOR;
//        _label_female.textColor = OLD_LIGHTGRAY;
//        role = MALE;
//    }
//    else
//    {
//        [_btn_female setImage:[UIImage imageNamed:@"性别选中"] forState:UIControlStateNormal];
//        [_btn_male setImage:[UIImage imageNamed:@"性别未选中"] forState:UIControlStateNormal];
//        _label_female.textColor = OLD_COLOR;
//        _label_male.textColor = OLD_LIGHTGRAY;
//        role = FEMALE;
//    }
//}

- (void)gotoNext:(id)sender
{
//    oldSupporterViewController *vc= [[oldSupporterViewController alloc] initWithNibName:@"oldSupporterViewController" bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
    if (![oldRegex isMobileNo:_text_phoneNum.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    if (![oldRegex isSMSCode:_text_validcode.text]) {
        [oldHub warningUser:@"请输入正确的验证码"];
        return;
    }
    
    if (![oldRegex isValidatePassword:_text_password.text]) {
        [oldHub warningUser:@"请输入数字或者字母组合的密码"];
        return;
    }
    
    if (_text_password.text != _text_confirm.text) {
        [oldHub warningUser:@"两次密码输入不同"];
        return;
    }
    
//    if (![oldRegex isChineseName:_text_name.text])
//    {
//        [oldHub warningUser:@"请输入正确的名字"];
//        return;
//    }
    
//    if (!role) {
//        [oldHub warningUser:@"请选择性别"];
//        return;
//    }
//    
//    if (!_text_age.text) {
//        [oldHub warningUser:@"请输入年龄"];
//        return;
//    }
    
    if (![oldRegex isValidateIDNumber:_text_certification.text]) {
        [oldHub warningUser:@"请输入正确的身份证号码"];
        return;
    }
    
    [self user_register];
}

- (void)sendSMS:(id)sender
{
    if (![oldRegex isMobileNo:_text_phoneNum.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    [self delayForGetCode];
    
    [oldHub waiting:@"验证码发送中"];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:_text_phoneNum.text ,@"mobile", ROLE, @"role", @"1", @"method", nil];
    
    [OLDNetWork httpRequestWithPostPut:URL_Register_SMS params:dict success:^(NSDictionary *dic) {
        [oldHub hideHUD];
        
    } error:^(NSString *error) {
        [oldHub hideHUD];
        [oldHub warningUser:error];
    }];
}

- (void)user_register
{
    NSString *password = [oldRegex md5:_text_password.text];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: _text_phoneNum.text, @"mobile", _text_validcode.text, @"code", password, @"password", _text_name.text, @"uname", _text_certification.text, @"idNum", @"ios", @"deviceType", ROLE, @"role", nil];
    
    NSString *cid = [[NSUserDefaults standardUserDefaults] objectForKey:GT_CID];
    if (cid) {
        [dict setObject:cid forKey:@"cid"];
    }
    if ([oldKeyChain getIDFA]) {
        [dict setObject:[oldKeyChain getIDFA] forKey:@"deviceId"];
    }
    NSString* deviceName = [[UIDevice currentDevice] systemName];
    if (deviceName) {
        [dict setObject:deviceName forKey:@"deviceType"];
    }
    
    WeakSelf
    
    [oldHub waiting:@"注册中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_Register_mobileRegister params:dict success:^(NSDictionary *dic) {
        [oldHub hideHUD];
        
        [OLDNetWork setToken:dic[@"token"]];
        [OLDNetWork setAFHTTPSessionManagerToken:dic[@"token"]];
        [ACCOUNT_SINGLETON initAccountByDic:dic];
        
        [AppDelegate gotoTab];
        
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

#pragma mark - timer
- (void)delayForGetCode
{
    next_time = 60;
    
    [self SmsCanClick:NO];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setDelay:) userInfo:_timer repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
    [_timer fire];
}

- (void)setDelay:(id)sender
{
    next_time--;
    if (next_time == 0) {
        [self SmsCanClick:YES];
        
        _timer.fireDate = [NSDate distantFuture];
        [_timer invalidate];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"重新发送(%ld)", (long)next_time];
    _btn_validcode.titleLabel.text = str;
    [_btn_validcode setTitle:str forState:UIControlStateNormal];
}

- (void)SmsCanClick:(BOOL)can
{
    if (can) {
        _btn_validcode.userInteractionEnabled = YES;
        _btn_validcode.alpha = 1.0;
        _btn_validcode.titleLabel.text = @"获取验证码";
        [_btn_validcode setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else
    {
        _btn_validcode.userInteractionEnabled = NO;
        _btn_validcode.alpha = 0.4;
    }
}

@end
